package com.two.lbms.controller;

import com.two.lbms.base.BaseController;
import com.two.lbms.model.HomeData;
import com.two.lbms.service.HomeService;
import com.two.lbms.service.UserService;
import com.two.lbms.utils.CookieUtil;
import com.two.lbms.utils.LoginCaptchaUtil;
import com.two.lbms.vo.Admin;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/28 14:43
 */
@Controller
@Slf4j
public class PageController extends BaseController {
    @Resource
    private HomeService homeService;
    @Resource
    private UserService userService;

    @RequestMapping("/index")
    public String toIndex(HttpServletRequest request) {
        String admin = CookieUtil.getCookieValue(request, "admin");
        request.setAttribute("admin", admin);
        return "index";
    }
    @RequestMapping("/login")
    public String toLogin() {
        return "login";
    }
    @RequestMapping("/captcha")
    public void getCaptcha(HttpServletResponse response){
        LoginCaptchaUtil.generateCaptcha(response);
    }

    @RequestMapping("/welcome")
    public String toWelcome(HttpServletRequest request){
        HomeData homeData = homeService.getHomeData();
        log.info("首页后台数据 : {}",homeData);
        request.setAttribute("homeData",homeData);
        return "welcome";
    }
    @RequestMapping("user/password")
    public String toPasswordPage(){
        return "user-password";
    }
    @RequestMapping("user/setting")
    public String toSettingPage(HttpServletRequest request){
        String admin = CookieUtil.getCookieValue(request, "admin");
        Admin user = userService.queryUserByName(admin);
        request.setAttribute("user", user);
        return "user-setting";
    }
}
